<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="../../common/import.jsp" %>
<%@ taglib uri="/WEB-INF/privilege" prefix="p"%>
<html>
  <head>
     <style type="text/css">
     </style>
  </head>
  <body scroll="no">
  <link href="${ctx}/resources/js/ztree/zTreeStyle.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript" src="${ctx}/resources/js/ztree/jquery.ztree.core-3.4.min.js"></script>
  <script type="text/javascript">
       jQuery.ajaxSetup({cache: false});
       var setting = {
    		async: {
   				enable: true,
   				dataType: "json",
   				url:"${ctx}/sys/organ/getOrganTree.do"
   			},
   		    data: {
   		      key: {
   	   			name: "NAME"
   	   		  },
			  simpleData: {
				enable: true,
				idKey: "ID",
				pIdKey: "PARENT_ID"
			  }
			},
			callback: {
				onAsyncSuccess: zTreeOnAsyncSuccess,
				onClick: zTreeOnClick
			}
		};
       function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
    	   var treeObj = $.fn.zTree.getZTreeObj("organTree");
    	   treeObj.expandAll(true);
    	};
    	function zTreeOnClick(event, treeId, treeNode) {
    	    if(treeNode.isParent){
    	    	var params = $('#organTable').datagrid('options').queryParams;
        		params["parentId"] = treeNode.ID;
        		reloadGrid();
    	    }
    	};
       $(function(){
    	   $.fn.zTree.init($("#organTree"), setting );
    	   
    	   $("#organTable").datagrid({
    		   nowrap: false,
    		   triped: false,
    		   fitColumns: true,
    		   //singleSelect: true,
    		   //checkOnSelect: true,
    		   //selectOnCheck: true,
    		   url: '${ctx}/sys/organ/list.do',
    		   columns: [[
    		             //{field: 'ck', checkbox: true},
    		             {field: 'name', title: '机构名称', width: 120, align: 'center'},
    		             {field: 'code', title: '编码', width: 100, align: 'center'},
    		             {field: 'linkman', title: '联系人', width: 100, align: 'center'},
    		             {field: 'officeTel', title: '办公电话', width: 100, align: 'center'},
    		             {field: 'createUser', title: '创建者', width: 100, align: 'center'},
    		             {field: 'createTime', title: '创建时间', width: 110, align: 'center'},
    		             {field: 'updateTime', title: '更新时间', width: 110, align: 'center'},
    		             {field: 'operate', title: '操作', width: 110, align: 'left',
     		            	 formatter:function(value,rowData,rowIndex){
     		            		 return '<p:op code="sysOrganEdit"><a class="a_op_bt" href="${ctx}/sys/organ/toAdd.do" onclick="return edit(this.href, '+rowData.id+');">修改</a></p:op>'
     		            		 +'&nbsp;<p:op code="sysOrganDel"><a class="a_op_bt" href="${ctx}/sys/organ/delete.do" onclick="return delOne(this.href,'+rowData.id+');">删除</a></p:op>'
     		            		 +'&nbsp;<p:op code="sysOrganView"><a class="a_op_bt" href="${ctx}/sys/organ/toView.do" onclick="return view(this.href, '+rowData.id+');">查看</a> </p:op>';
     		                 }
    		             }
    		            ]],
		       queryParams: {
		          parentId: ''
		       },
    		   pagination: true,
    		   rownumbers: true,
    		   pageSize: 20,
    		   pageList: pageList//,
    		   //toolbar: '#btabs'
    	   });
       });
       function add(href){
    	   showWindow("添加组织机构", href);
    	   return false;
       }
       /*function selectEdit(href){
    	   var node = getSelectedRows();
    	   if (node&&node.length==1){
    		   //showWindow("修改组织机构", href+"?id="+node[0].id);
    		   edit(href, node[0].id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }*/
       function edit(href, id){
    	   if(id!=undefined&&id!=''){
    		   showWindow("修改组织机构", href+"?id="+id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function delOne(href, id){
    	   if(id!=undefined&&id!=''){
    		   showConfirm("温馨提示", "确定要删除组织机构吗?", function(){
    			   $.blockUI();
    			   $.ajax({
		    			  url: href,   // 提交的页面
		    		      data: {id: id}, 
		    		      type: "POST",
		    		      success: function(data) {
		    		    	  $.unblockUI();
		    		        	if(data.success==true){
		    		        		showSuccessMsg();
		    		        		reloadGrid();
		    		        		reloadTree();
		    		        	}else{
		    		        		showFailAlert(data.msg)
		    		        	}
		    		      },
		    		      error: function(xhr) {
		    		    	  $.unblockUI();
		    		    	  showFailAlert2(xhr);
		    		      }
		    		  })
    		   });
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       /*
       function selectView(href){
    	   var node = getSelectedRows();
    	   if (node&&node.length==1){
    		   //showWindow("查看组织机构", href+"?id="+node[0].id);
    		   view(href, node[0].id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }*/
       function view(href, id){
    	   if(id!=undefined&&id!=''){
    		   showWindow("查看组织机构", href+"?id="+id);
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function reloadGrid(){
    	   $("#organTable").datagrid("reload");
       }
       function loadGrid(){
    	   $("#organTable").datagrid("load");
       }
       function reloadTree(){
    	   var treeObj = $.fn.zTree.getZTreeObj("organTree");
		   treeObj.reAsyncChildNodes(null, "refresh");
       }
       function getSelectedRows(){
    		var rows = $('#organTable').datagrid('getSelections');
    		return rows;
       }
       function queryForm(){
    	   var params = $('#organTable').datagrid('options').queryParams; //先取得 datagrid 的查询参数
    	   var fields =$('#queryForm').serializeArray(); //自动序列化表单元素为JSON对象
    	   $.each( fields, function(i, field){
    		   params[field.name] = field.value; //设置查询参数
           });   
    	   loadGrid();
       }
       function clearForm(){
    	   $('#queryForm').form('clear');
    	   queryForm();
       }
    </script>
    <div class="easyui-layout" data-options="fit:true">
    <div region="west" split="true" title="组织机构树" style="padding: 1px; width:220px;" border="false">
       <ul id="organTree" class="ztree"></ul>
    </div>
    <div region="center" style="padding: 0px;" border="false">
        <div class="easyui-layout" data-options="fit:true">
           <div region="north" style="overflow-y: hidden; overflow-x: hidden" border="false">
		        <form id="queryForm" onSubmit="queryForm();return false;">
			        <table width="99.5%" style="padding-left: 5px;">
				        <td width="60px">机构名称</td><td width="60px"><input id="organName" type="text" name="organName" value="" ></td>
					    <td width="85px"><a href="#" class="easyui-linkbutton" iconcls="icon-search" onclick="queryForm();">查询</a></td>
					    <td><a href="#" class="easyui-linkbutton" iconcls="icon-search" onclick="clearForm();">重置</a></td>
					</table>
		        </form>
		        <div id="btabs" class="btabs">
				    <p:op code="sysOrganAdd">
				    	<a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/organ/toAdd.do" onclick="return add(this.href);"><span class="icon icon-add">&nbsp;</span>添加</a> 
				    </p:op>
				    <%--<p:op code="sysOrganEdit">
						<span class="icon icon-edit">&nbsp;</span><a href="${ctx}/sys/organ/toAdd.do" onclick="return selectEdit(this.href);">修改</a> 
					</p:op>
				    <p:op code="sysOrganDel">
						<span class="icon icon-delete">&nbsp;</span><a href="${ctx}/sys/organ/delete.do" onclick="return selectDel(this.href);">删除</a> 
					</p:op>
				    <p:op code="sysOrganView">
						<span class="icon icon-view">&nbsp;</span><a href="${ctx}/sys/organ/toView.do" onclick="return selectView(this.href);">查看</a> 
					</p:op>--%>
				</div>
		    </div>
		    <div region="center" style="padding: 0px;" border="false">
		       <table id="organTable" fit="true"></table>
		    </div>
        </div>
    </div>
    </div>
  </body>
</html>
